package DTI;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

public class IndexGenerator {
	public static final String DELIMITER = "==";
	public static final String ENDING_DELIMITER = "==";
	
	public static void createIndex(Corpus corpus, Options options) throws IOException, FileNotFoundException{
		
		String todays_date_and_time, break_chars, stop_word_file,
		       total_corpus_char_size, total_corpus_word_size, 
			   total_corpus_line_size, total_number_corpus_files;
		
		String complete_header = "";
				
		// Getting today's date as a string
		Date today = new Date();
		todays_date_and_time = today.toString();
		
		// Getting the break characters used.
		break_chars = options.getBreaks();		
		
		// Getting the stop word file name.
		if(options.getWordAnalyzer() == null){
			stop_word_file = "";
		} else {
			stop_word_file = options.getWordAnalyzer().getStopFileLocation();
		}
		
		//getting filenames.
		Vector corpus_files = corpus.getCorpus_files();
		
		String file_name = "";
		
		for (Iterator i = corpus_files.iterator(); i.hasNext();) {
			CorpusFile file = (CorpusFile) i.next();
			file_name = file_name + DELIMITER + file.getFileName();
					
		}
		
		//getting word, file, character count
		total_corpus_char_size = "" + corpus.getCharCount();
		total_corpus_word_size = "" + corpus.getWordCount();
		total_number_corpus_files = "" + corpus.getFileCount();
		
		// Putting all of the static header information together
		complete_header = todays_date_and_time + DELIMITER + break_chars + DELIMITER +
			              stop_word_file + DELIMITER + total_corpus_char_size + DELIMITER + 
						  total_corpus_word_size + DELIMITER + total_number_corpus_files + file_name + ENDING_DELIMITER;
		
		
		// Time for File I/O.
		FileOutputStream stream = new FileOutputStream("DTI.out");
		ObjectOutputStream oStream = new ObjectOutputStream(stream);
		// Write the header to file (easy enough)
		oStream.writeChars(complete_header);
		
		// Write all the hash maps to file 
		oStream.writeObject(corpus.getCorpus_words());
			
		// Should be done.
		stream.close();
		oStream.close();
	}

	
}
